package demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class NewRestaurantRegistration extends HttpServlet {
	private PreparedStatement pstmt;	// Prepared Statement for update
	
	/**
	 * Constructor of the object.
	 */
	public NewRestaurantRegistration() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		
		// Put your code here
		
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		
		// Obtain parameter from the client
	    String name = request.getParameter("name");
	    String location = request.getParameter("location");
	    String phone = request.getParameter("phone");
	    String type = request.getParameter("type");

	    try {
	    	if ( name.length() == 0 || location.length() == 0 || phone.length() == 0 || type.length() == 0) {
	    		out.println("Please fill in all the required information!");
	    		return;		// End the method
	    	}
	    	if ( registerRestaurant(name, location, phone, type ) != 0)
	    		out.println( "<p> Registration Succeeded! </p>" );
	    	else 
	    		out.println( "<p> Registration Failed! </p>" );
	    } catch( Exception ex ) {
	    	out.println( "<p> Registration Failed! </p>" );
	    } finally {
	    	//out.flush();
	    	out.println("  </BODY>");
			out.println("</HTML>");
	    	out.close(); 	// Close stream
	     }
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		jdbcInit();
	}
	
	/**
	 * initialization of the JDBC connection.
	 */
	
	private void jdbcInit() {
		try {
	    	  // Declare driver and connection string
	    	  String driver = "com.mysql.jdbc.Driver";
	    	  String connectionString = "jdbc:mysql://localhost:3306/ordering_system?" +
					"user=root&" +
					"password=89djm0210";
	    	  
	    	  // Load the driver
	    	  Class.forName( driver );
	    	  
	    	  // Connect to the database
	    	  Connection conn = DriverManager.getConnection( connectionString );

	    	  // Create a Statement
	    	  pstmt = conn.prepareStatement( "insert into restaurants values ( ?, ?, ?, ? )" );
	      } catch ( Exception ex ) {
	    	  ex.printStackTrace();
	      }
	}
	
	/** 
	 * Store a student record to the database 
	 * 
	 * @param name the new restaurant name
	 * @param location the new restaurant's location
	 * @param phone the new restaurant's phone number
	 * @param type the new restaurant's food type
	 * 
	 * @return int the number of the rows affected in the update statement
	 * 
	 * @throws SQLException if an error occurs
	 */
    private int registerRestaurant( String name, 
    		String location, 
    		String phone, 
    		String type ) throws SQLException {
    	pstmt.setString( 1, name );
    	pstmt.setString( 2, location );
    	pstmt.setString( 3, phone );
    	pstmt.setString( 4, type );
    	
    	return pstmt.executeUpdate();
    }

}
